package kechengsheji;

import javax.swing.*;



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class Home extends JFrame {

    Font font = new Font("微软雅黑",Font.BOLD,20);
    Font font1 = new Font("微软雅黑",Font.BOLD,17);
    String[] strings = {"校门","1号楼","2号楼","3号楼","4号楼","食堂","宿舍","图书馆"};
    String str1="";
	int m=0;
	int start,end;
	MGraph G = GenerateGraph.generateMGraph();
	ShortestPath_DIJ2 dij = new ShortestPath_DIJ2();


    public Home(){

        setTitle("地图导航系统");
        setDefaultCloseOperation(3);
        setResizable(false);
        setBounds(100,50,930,630);
        setLayout(null);
        setLocationRelativeTo(null);

        //左侧面板
        JPanel jPanel = new JPanel(){
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.drawLine(300,95,300,130);//校门到1号楼
                g.drawLine(300,170,300,500);//1号楼到食堂
                g.drawLine(250,150,200,220);//1号楼到2号楼
                g.drawLine(150,240,150,280);//2号楼到3号楼
                g.drawLine(150,320,150,360);//3号楼到4号楼
                g.drawLine(150,400,250,500);//4号楼到食堂
                g.drawLine(350,520,430,290);//食堂到宿舍
                g.drawLine(350,150,430,250);//1号楼到宿舍
                g.drawLine(70,500,150,400);//4号楼到图书馆
                g.drawLine(250,520,120,520);//食堂到图书馆
            }
        };
        jPanel.setLayout(null);
        jPanel.setBounds(0,0,520,630);
        jPanel.setBackground(Color.white);

        /**
         * 8个建筑按钮
         */
        JButton schoolgate = new JButton("校门");
        schoolgate.setFont(font);
        schoolgate.setBounds(250,55,100,40);
        schoolgate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new schoolgateFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(schoolgate);

        JButton first = new JButton("1号楼");
        first.setFont(font);
        first.setBounds(250,130,100,40);
        first.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new firstFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(first);

        JButton second = new JButton("2号楼");
        second.setFont(font);
        second.setBounds(100,200,100,40);
        second.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new secondFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(second);

        JButton third = new JButton("3号楼");
        third.setFont(font);
        third.setBounds(100,280,100,40);
        third.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new thirdFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(third);

        JButton forth = new JButton("4号楼");
        forth.setFont(font);
        forth.setBounds(100,360,100,40);
        forth.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new forthFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(forth);

        JButton canteen = new JButton("食堂");
        canteen.setFont(font);
        canteen.setBounds(250,500,100,40);
        canteen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new canteenFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(canteen);

        JButton dorm = new JButton("宿舍");
        dorm.setFont(font);
        dorm.setBounds(380,250,100,40);
        dorm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new dormFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(dorm);

        JButton library = new JButton("图书馆");
        library.setFont(font);
        library.setBounds(20,500,100,40);
        library.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new libraryFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(library);
        
        JButton laboratory = new JButton("实验楼");
        laboratory.setFont(font);
        laboratory.setBounds(30,55,100,40);
        laboratory.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new laboratoryFrame().init();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        jPanel.add(laboratory);

        /*
         * 距离标签
         */
        JLabel a = new JLabel("50");
        a.setFont(font1);
        a.setBounds(320,100,60,35);
        jPanel.add(a);

        JLabel b = new JLabel("100");
        b.setFont(font1);
        b.setBounds(190,160,60,35);
        jPanel.add(b);

        JLabel c = new JLabel("50");
        c.setFont(font1);
        c.setBounds(170,240,60,35);
        jPanel.add(c);

        JLabel d = new JLabel("50");
        d.setFont(font1);
        d.setBounds(170,320,60,35);
        jPanel.add(d);

        JLabel e = new JLabel("150");
        e.setFont(font1);
        e.setBounds(400,180,60,35);
        jPanel.add(e);

        JLabel f = new JLabel("300");
        f.setFont(font1);
        f.setBounds(310,330,60,35);
        jPanel.add(f);

        JLabel g = new JLabel("300");
        g.setFont(font1);
        g.setBounds(400,380,60,35);
        jPanel.add(g);

        JLabel h = new JLabel("130");
        h.setFont(font1);
        h.setBounds(160,520,60,35);
        jPanel.add(h);

        JLabel i = new JLabel("150");
        i.setFont(font1);
        i.setBounds(70,435,60,35);
        jPanel.add(i);

        JLabel j = new JLabel("150");
        j.setFont(font1);
        j.setBounds(210,430,60,35);
        jPanel.add(j);

        add(jPanel);

        JLabel qd = new JLabel("起点 :");
        qd.setBounds(550,50,60,40);
        qd.setFont(font);
        add(qd);

        JTextField jTextField = new JTextField();
        jTextField.setBounds(620,50,200,40);
        jTextField.setFont(font);
        add(jTextField);

        JLabel zd = new JLabel("终点 :");
        zd.setBounds(550,130,60,40);
        zd.setFont(font);
        add(zd);

        JTextField jTextField1 = new JTextField("");
        jTextField1.setBounds(620,130,200,40);
        jTextField1.setFont(font);
        add(jTextField1);

        JTextArea textArea = new JTextArea();
        textArea.setColumns(15); // 设置文本域显示文字的列数
		textArea.setRows(3); // 设置文本域显示文字的行数
        textArea.setLineWrap(true);
        textArea.setFont(font);
        textArea.setEditable(false);
        JScrollPane jScrollPane = new JScrollPane(textArea);
        jScrollPane.setBounds(590,320,250,200);
        jScrollPane.setVerticalScrollBarPolicy(22);
        add(jScrollPane);
       
        

        JButton jButton = new JButton("查找路线");
        jButton.setBounds(650,220,140,45);
        jButton.setFont(font);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            	textArea.setText("");
            	int m = jTextField.getText().length();
                int n = jTextField1.getText().length();
                if(m==0&&n==0){JOptionPane.showMessageDialog(
                        null,"输入位置不能为空！"
                );return;}
                if(m==0){JOptionPane.showMessageDialog(
                        null,"起始位置不能为空！");return;}
                if(n==0){JOptionPane.showMessageDialog(
                        null,"终点位置不能为空！"
                );return;}
                if(jTextField.getText().equals("实验楼")){JOptionPane.showMessageDialog(
                        null,"实验楼不能到达"+jTextField1.getText()+"！");return;}
                if(jTextField1.getText().equals("实验楼")){JOptionPane.showMessageDialog(
                        null,jTextField.getText()+"不能到达实验楼！"
                );return;}
                int a = 0,b=0;
                for(int i=0;i<8;i++){
                    if(jTextField.getText().equals(strings[i])){
                    	a++; start=i;                  	
                		             		
                    	}
                    if(jTextField1.getText().equals(strings[i])){
                    	b++;end=i;
                    
                    	}
                }
                if(jTextField.getText().equals(jTextField1.getText())){JOptionPane.showMessageDialog(
                        null,"始末位置不能一致！");return;}
                if(a==0&&b==0){JOptionPane.showMessageDialog(null,
                        "输入位置有误！");return;}
                if(a==0){JOptionPane.showMessageDialog(null,"起始位置不存在！");return;}
                if(b==0){JOptionPane.showMessageDialog(null,"终点位置不存在！");return;}
                dij.DIJ(G,start);
            	try {
					str1=dij.shortline(G,end);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				m=dij.beeline(G,end);
				textArea.setText("最优路径:  "  +str1+  "\n"+"距离：  "+m + "m"+"\n"+"预计需要"+m/50+"分钟" );
                
            }
        });
       
        add(jButton);

        repaint();
    }

    //初始化
    void init(){

        setVisible(true);

    }

    public static void main(String[] args) {
        new Home().init();

    }


}
